package com.maslyk.configuration;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

public class ConfigurationManager
{
    private static ConfigurationManager instance;
    
    // system
    private static String INVITATION_PROPERTIES_FILE = "./invitation.properties";
    private static Properties config = null;

    // konfiguracja email sendera
    private static final String EMAIL_SERVICE_ENABLED           = "email.service.enabled";
    private static final String DEFAULT_EMAIL_SENDER_ADDRESS    = "default.email.sender.address";
    private static final String DEFAULT_EMAIL_SUBJECT           = "default.email.subject";
    private static final String EMAIL_SMTP_HOST                 = "default.email.smtp.host";
    private static final String EMAIL_SMTP_PORT                 = "default.email.smtp.port";
    private static final String EMAIL_SMTP_AUTH                 = "default.email.smtp.auth";
    private static final String EMAIL_SMTP_USER                 = "default.email.smtp.user";
    private static final String EMAIL_SMTP_PASSWORD             = "default.email.smtp.password";
    private static final String PRINTOUT_CONFIG_DIR             = "printout.config.dir";
    private static final String EMAIL_PROCESSING_PERIOD         = "email.processing.period.minutes";

    // konfiguracja cen za zaproszenia
    private static final String SHIPPING_COST                   = "shipping.cost";
    private static final String SHIPPING_EMAIL_CONTENT_FILE     = "shipping.email.content.file";
    private static final String SHIPPING_EMAIL_SUBJECT          = "shipping.email.subject";
    private static final String OTHER_COST                      = "other.cost";
    private static final String CUTTING_NAMES                   = "cutting.names";
    private static final String INVITATION_PRICE                = "invitation.price";
    private static final String ITEM_PERSONALIZE_PRICE          = "invitation.personalize.price";
    private static final String ITEM_ENVELOPE_PERSONALIZE_PRICE = "invitation.envelope.personalize.price";
    private static final String PAPER_CHANGE_UNIT_PRICE         = "paper.change.unit.price";

    public ConfigurationManager()
    {
        config = new Properties();

        InputStream urlStream = null;
        try
        {
            ClassLoader cl = ConfigurationManager.class.getClassLoader();
            URL url = cl.getResource(INVITATION_PROPERTIES_FILE);
            if (url == null)
            {
                System.out.println("ERROR: File '" + INVITATION_PROPERTIES_FILE + "' is missing.");
            }

            urlStream = url.openStream();
            config.load(urlStream);

        } 
        catch (IOException ex)
        {
        	System.out.println(ex);
        } 
        finally
        {
            try
            {
                urlStream.close();
            }
            catch (IOException ex)
            {
            	System.err.println("ERROR: "+ex);
            }
        }
    }

    public static synchronized ConfigurationManager getInstance()
    {
        if(instance == null)
        {
            instance = new ConfigurationManager();
            return instance;
        }
        else
            return instance;

    }
    
    public String getDefaultSenderEmailAddress()
    {
        return config.getProperty(DEFAULT_EMAIL_SENDER_ADDRESS, "");
    }
    
    public String getDefaultEmailSubject()
    {
        return config.getProperty(DEFAULT_EMAIL_SUBJECT, "");
    }
    
    public String getEmailSMTPHostAddress()
    {
        return config.getProperty(EMAIL_SMTP_HOST, "");
    }
    
    public String getEmailSMTPHostPort()
    {
        return config.getProperty(EMAIL_SMTP_PORT, "");
    }
        
    public String getEmailSMTPAuth()
    {
        return config.getProperty(EMAIL_SMTP_AUTH, "false");
    }
    
    public String getEmailSMTPUser()
    {
        return config.getProperty(EMAIL_SMTP_USER, "");
    }
                
    public String getEmailSMTPPassword()
    {
        return config.getProperty(EMAIL_SMTP_PASSWORD, "");
    }
    
    public boolean getEMAIL_SERVICE_ENABLED()
    {
        return "true".equals(config.getProperty(EMAIL_SERVICE_ENABLED,null));
    }

    public Double getSHIPPING_COST()
    {
        return Double.valueOf(config.getProperty(SHIPPING_COST, null));
    }

    public Double getOTHER_COST()
    {
        return Double.valueOf(config.getProperty(OTHER_COST, "0.0"));
    }

    public Double getCUTTING_NAMES()
    {
        return Double.valueOf(config.getProperty(CUTTING_NAMES, null));
    }

    public String getINVITATION_PRICE()
    {
        return config.getProperty(INVITATION_PRICE, null);
    }

    public Double getITEM_PERSONALIZE_PRICE()
    {
        return Double.valueOf(config.getProperty(ITEM_PERSONALIZE_PRICE, null));
    }

    public Double getITEM_ENVELOPE_PERSONALIZE_PRICE()
    {
        return Double.valueOf(config.getProperty(ITEM_ENVELOPE_PERSONALIZE_PRICE, null));
    }
    
    public Double getPAPER_CHANGE_UNIT_PRICE()
    {
        return Double.valueOf(config.getProperty(PAPER_CHANGE_UNIT_PRICE, null));
    }

    public String getPRINTOUT_CONFIG_DIR()
    {
        return config.getProperty(PRINTOUT_CONFIG_DIR,".");
    }

    public String getSHIPPING_EMAIL_CONTENT_FILE()
    {
        return config.getProperty(SHIPPING_EMAIL_CONTENT_FILE,".");
    }

    public String getSHIPPING_EMAIL_SUBJECT()
    {
        return config.getProperty(SHIPPING_EMAIL_SUBJECT,".");
    }

    public int getEMAIL_PROCESSING_PERIOD()
    {
        return Integer.valueOf(config.getProperty(EMAIL_PROCESSING_PERIOD, "60")).intValue();
    }
    
    public boolean isAutomaticNumberGeneration()
    {
    	return Boolean.valueOf(config.getProperty("automatic.number.generation.enable", "true"));
    }
    
    public String getAttachmentDir()
    {
    	return config.getProperty("attachment.dir", "attachment");
    }
}
